package cn.maxhou.service.impl;

import cn.maxhou.entity.Storage;
import cn.maxhou.entity.SysUser;
import cn.maxhou.mapper.StorageMapper;
import cn.maxhou.service.StorageService;
import cn.maxhou.util.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("storageService")
public class StorageServiceImpl implements StorageService {
    private static final Logger LOGGER= LoggerFactory.getLogger(StorageServiceImpl.class);

    @Autowired
    private StorageMapper StorageMapper;


    @Override
    public Page<Storage> findPage(Storage entity, Integer pageNo, Integer pageSize) throws Exception {
        Page<Storage>page=null;
        try{
            page=new Page<Storage>();
            page.setPageNo(pageSize);
            //查询总数,计算总页数
            Long count = StorageMapper.count(entity);
            page.setTotalCount(count.intValue());
            page.setPageNo(pageNo);
            //查询分页数据
            List<Storage> data =StorageMapper.getPage(entity,page.offset(), page.getPageSize());
            page.setData(data);
        }catch (Exception e){
            LOGGER.error("执行SysUserServiceImpl.findPage(SysUser,integer,integer)方法出现异常.",e);
            throw e;
        }
        return page;
    }

    @Override
    public boolean saveOrUpdate(Storage entity) throws Exception {
        try{
            if(entity.getId()==null){
                //无主键则新增
                return StorageMapper.insert(entity)>0;
            }else {
                //有主键则修改
                return StorageMapper.update(entity)>0;
            }
        }catch (Exception e){
            LOGGER.error("执行SysUserServiceImpl.saveOrUpdate(SysUser)方法出现异常.",e);
            throw e;
        }
    }

    @Override
    public Storage findById(Long id) throws Exception {
        try{
            return StorageMapper.getById(id);
        }catch (Exception e){
            LOGGER.error("执行SysUserServiceImpl.findById(Long)方法出现异常.",e);
            throw e;
        }
    }

    @Override
    public int deleteById(Long id) throws Exception {
        try{
            return StorageMapper.deleteById(id);
        }catch (Exception e){
            LOGGER.error("执行SysUserServiceImpl.deleteById(Long)方法出现异常.",e);
            throw e;
        }
    }

    @Override
    public Storage getBysrCode(String srCode) {
        return StorageMapper.getBysrCode(srCode);
    }
}
